package com.msh.frame.client.model;

import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 线程上下文
 */
@Data
@ToString
@Accessors(chain = true)
public class ThreadContext {

  private Long accountId;
  private String traceId;
  private Long tenantId;


  public static Long accountIdGet(ThreadLocal<ThreadContext> threadLocal) {
    ThreadContext threadContext = threadLocal.get();
    if (null == threadContext) {
      return null;
    }
    return threadContext.getAccountId();
  }

  public static String traceIdGet(ThreadLocal<ThreadContext> threadLocal) {
    ThreadContext threadContext = threadLocal.get();
    if (null == threadContext) {
      return null;
    }
    return threadContext.getTraceId();
  }

  public static Long tenantIdGet(ThreadLocal<ThreadContext> threadLocal) {
    ThreadContext threadContext = threadLocal.get();
    if (null == threadContext) {
      return null;
    }
    return threadContext.getTenantId();
  }

  public static ThreadContext accountIdSet(Long accountId, ThreadLocal<ThreadContext> threadLocal) {
    ThreadContext threadContext = threadLocal.get();
    if (null == threadContext) {
      threadContext = new ThreadContext();
      threadLocal.set(threadContext);
    }
    threadContext.setAccountId(accountId);
    return threadContext;
  }

  public static ThreadContext traceIdSet(String traceId, ThreadLocal<ThreadContext> threadLocal) {
    ThreadContext threadContext = threadLocal.get();
    if (null == threadContext) {
      threadContext = new ThreadContext();
      threadLocal.set(threadContext);
    }
    threadContext.setTraceId(traceId);
    return threadContext;
  }

  public static ThreadContext tenantIdSet(Long tenantId, ThreadLocal<ThreadContext> threadLocal) {
    ThreadContext threadContext = threadLocal.get();
    if (null == threadContext) {
      threadContext = new ThreadContext();
      threadLocal.set(threadContext);
    }
    threadContext.setTenantId(tenantId);
    return threadContext;
  }

}
